package com.putao.utils;

import com.beust.jcommander.internal.Lists;
import com.putao.constants.Constants;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringEscapeUtils;

//import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public abstract class StringUtils extends org.apache.commons.lang3.StringUtils {

	// private static Logger logger = LoggerFactory.getLogger(StringUtils.class);

	private static final char SEPARATOR = '_';
	private static final char D = ',';
	private static final String CHARSET_NAME = "UTF-8";

	public static String generateCode(){
		Random random = new Random();
		String fourRandom = random.nextInt(10000) + "";
		int randLength = fourRandom.length();
		if(randLength < 4){
			for(int i = 1; i <= 4 - randLength; i++)
				fourRandom = "0" + fourRandom ;
		}
		return fourRandom;
	}

	public static String getUuid() {
		return UUID.randomUUID().toString();
	}



	public static int calculateLength(String name) {
		if (StringUtils.isBlank(name)) {
			return -1;
		}
		int length = 0;
		Pattern pattern = Pattern.compile("[a-zA-Z0-9-]");
		for (String s : name.split("")) {
			length += pattern.matcher(s).find() ? Constants.CHAR_LENGTH_OF_ENGLISH : Constants.CHAR_LENGTH_OF_CHINESE;
		}
		return length;
	}

	/**
	 * 获取订单编号
	 * @param string  前缀
	 * @return
	 */
	public synchronized static String getBillCode(String string) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date newDate = new Date();
		// String类型的当前时间戳
		String stringTime = String.valueOf(newDate.getTime());
		return string+sdf.format(newDate) + stringTime.substring(stringTime.length()-4,stringTime.length());
	}

	/**
	 * 将Sting数组 转换为List<String>
	 *
	 * @param strs
	 * @return
	 */
	public static List<String> convertStrsToList(String[] strs) {
		List<String> list = Lists.newArrayList();
		if (strs != null && strs.length > 0) {
			for (String string : strs) {
				list.add(string);
			}
		}
		return list;
	}

	/**
	 * 将数组转换为'1','2','3','4'
	 *
	 * @param strs
	 * @return
	 */
	public static String convertStrsToStr(String[] strs) {
		if (strs != null && strs.length > 0) {
			StringBuffer sb = new StringBuffer();
			for (String string : strs) {
				sb.append("'").append(string.trim()).append("'").append(D);
			}
			return sb.substring(0, sb.length() - 1);
		}
		return "'-1'";
	}

	/**
	 * 将list<String> 转换为1,2,3,4,5
	 *
	 * @param list
	 * @return
	 */
	public static String convertListToString(List<String> list) {
		if (list != null && list.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (String string : list) {
				sb.append(string).append(D);
			}
			return sb.toString().substring(0,sb.length()-1);
		}
		return "";
	}

	/**
	 * 将Set<String> 转换为1,2,3,4,5
	 *
	 * @param list
	 * @return
	 */
	public static String convertSetToString(Set<String> list) {
		if (list != null && list.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (String string : list) {
				sb.append(string).append(D);
			}
			return sb.toString();
		}
		return "";
	}

	/**
	 * 将1,2,3,a,b,c 转换为List<String>
	 *
	 * @param str
	 * @return
	 */
	public static List<String> convertList(String str) {
		List<String> list = Lists.newArrayList();
		if (isNotBlank(str)) {
			String[] s = str.split(String.valueOf(D));
			for (String string : s) {
				list.add(string);
			}
		}
		return list;
	}

	/**
	 * 将1,2,3,a,b,c 转换为 "1","2","3","a","b","c"
	 *
	 * @param str
	 * @return
	 */
	public static String convertStr(String str) {
		if (isNotBlank(str)) {
			StringBuilder sb = new StringBuilder();
			String[] s = str.split(String.valueOf(D));
			for (String string : s) {
				sb.append("'").append(string).append("'").append(D);
			}
			return sb.substring(0, sb.length() - 1);
		}
		return "";
	}

	/**
	 * 转换为字节数组
	 *
	 * @param str
	 * @return
	 */
	public static byte[] getBytes(String str) {
		if (str != null) {
			try {
				return str.getBytes(CHARSET_NAME);
			} catch (UnsupportedEncodingException e) {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 转换为Boolean类型 'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will
	 * return true. Otherwise, false is returned.
	 */
	public static Boolean toBoolean(final Object val) {
		if (val == null) {
			return false;
		}
		return BooleanUtils.toBoolean(val.toString()) || "1".equals(val.toString());
	}

	/**
	 * 转换为字节数组
	 * @return
	 */
	public static String toString(byte[] bytes) {
		try {
			return new String(bytes, CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			return EMPTY;
		}
	}

	/**
	 * 如果对象为空，则使用defaultVal值 see: ObjectUtils.toString(obj, defaultVal)
	 *
	 * @param obj
	 * @param defaultVal
	 * @return
	 */
	public static String toString(final Object obj, final String defaultVal) {
		return obj == null ? defaultVal : obj.toString();
	}

	/**
	 * 是否包含字符串
	 *
	 * @param str
	 *            验证字符串
	 * @param strs
	 *            字符串组
	 * @return 包含返回true
	 */
	public static boolean inString(String str, String... strs) {
		if (str != null) {
			for (String s : strs) {
				if (str.equals(trim(s))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 替换掉HTML标签方法
	 */
	public static String replaceHtml(String html) {
		if (isBlank(html)) {
			return "";
		}
		String regEx = "<.+?>";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(html);
		String s = m.replaceAll("");
		return s;
	}
	/**
	 * 替换html的内容 &nbsp; 转换空格
	 * @return
	 */
	public static String replaceHtmlCode(String s) {
		if (isBlank(s)) {
			return null;
		}
		s = StringEscapeUtils.unescapeHtml4(s);
		return s;
	}


	/**
	 * 替换为手机识别的HTML，去掉样式及属性，保留回车。
	 *
	 * @param html
	 * @return
	 */
	public static String replaceMobileHtml(String html) {
		if (html == null) {
			return "";
		}
		return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
	}

	/**
	 * 替换为手机识别的HTML，去掉样式及属性，保留回车。
	 *
	 * @param txt
	 * @return
	 */
	/*
	 * public static String toHtml(String txt){ if (txt == null){ return ""; }
	 * return replace(replace(Encodes.escapeHtml(txt), "\n", "<br/>"), "\t",
	 * "&nbsp; &nbsp; "); }
	 */

	/**
	 * 缩略字符串（不区分中英文字符）
	 *
	 * @param str
	 *            目标字符串
	 * @param length
	 *            截取长度
	 * @return
	 */
	public static String abbr(String str, int length) {
		if (str == null) {
			return "";
		}
		try {
			StringBuilder sb = new StringBuilder();
			int currentLength = 0;
			for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
				currentLength += String.valueOf(c).getBytes("GBK").length;
				if (currentLength <= length - 3) {
					sb.append(c);
				} else {
					sb.append("...");
					break;
				}
			}
			return sb.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
	public static Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
	public static String abbr2(String param, int length) {
		if (param == null) {
			return "";
		}
		StringBuffer result = new StringBuffer();
		int n = 0;
		char temp;
		// 是不是HTML代码
		boolean isCode = false;
		// 是不是HTML特殊字符,如&nbsp;
		boolean isHtml = false;
		for (int i = 0; i < param.length(); i++) {
			temp = param.charAt(i);
			if (temp == '<') {
				isCode = true;
			} else if (temp == '&') {
				isHtml = true;
			} else if (temp == '>' && isCode) {
				n = n - 1;
				isCode = false;
			} else if (temp == ';' && isHtml) {
				isHtml = false;
			}
			try {
				if (!isCode && !isHtml) {
					n += String.valueOf(temp).getBytes("GBK").length;
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			if (n <= length - 3) {
				result.append(temp);
			} else {
				result.append("...");
				break;
			}
		}
		// 取出截取字符串中的HTML标记
		String tempResult = result.toString().replaceAll("(>)[^<>]*(<?)", "$1$2");
		// 去掉不需要结素标记的HTML标记
		tempResult = tempResult.replaceAll(
				"</?(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>",
				"");
		// 去掉成对的HTML标记
		tempResult = tempResult.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>", "$2");
		// 用正则表达式取出标记

		Matcher m = p.matcher(tempResult);
		List<String> endHtml = Lists.newArrayList();
		while (m.find()) {
			endHtml.add(m.group(1));
		}
		// 补全不成对的HTML标记
		for (int i = endHtml.size() - 1; i >= 0; i--) {
			result.append("</");
			result.append(endHtml.get(i));
			result.append(">");
		}
		return result.toString();
	}

	/**
	 * 转换为Double类型
	 */
	public static Double toDouble(Object val) {
		if (val == null) {
			return 0D;
		}
		try {
			return Double.valueOf(trim(val.toString()));
		} catch (Exception e) {
			return 0D;
		}
	}

	/**
	 * 转换为Float类型
	 */
	public static Float toFloat(Object val) {
		return toDouble(val).floatValue();
	}

	/**
	 * 转换为Long类型
	 */
	public static Long toLong(Object val) {
		return toDouble(val).longValue();
	}

	/**
	 * 转换为Integer类型
	 */
	public static Integer toInteger(Object val) {
		return toLong(val).intValue();
	}

	/**
	 * 获得i18n字符串
	 */
	/*
	 * public static String getMessage(String code, Object[] args) { //
	 * LocaleResolver localLocaleResolver = (LocaleResolver)
	 * SpringContextHolder.getBean(LocaleResolver.class); // Locale localLocale =
	 * localLocaleResolver.resolveLocale(request); // return
	 * SpringContextHolder.getApplicationContext().getMessage(code, args,
	 * localLocale); HttpServletRequest request =
	 * ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).
	 * getRequest(); Locale locale = RequestContextUtils.getLocale(request);
	 *
	 * logger.info("Locale:" + locale.getLanguage()); // String msg1 =
	 * messageSource.getMessage(code, null, Locale.CHINA); // logger.info("Msg1:" +
	 * msg1);
	 *
	 * String msg2 = messageSource.getMessage(code, args, locale);
	 * logger.info("Msg2:" + msg2); return msg2; }
	 */

	/**
	 * 获得用户远程地址
	 */
	public static String getRemoteAddr(HttpServletRequest request) {
		String remoteAddr = request.getHeader("X-Real-IP");
		if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("X-Forwarded-For");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("Proxy-Client-IP");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("WL-Proxy-Client-IP");
		}
		return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
	}

	/**
	 * 驼峰命名法工具
	 *
	 * @return toCamelCase("hello_world") == "helloWorld"
	 *         toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 *         toUnderScoreCase("helloWorld") = "hello_world"
	 */
	public static String toCamelCase(String s) {
		if (s == null) {
			return null;
		}

		s = s.toLowerCase();

		StringBuilder sb = new StringBuilder(s.length());
		boolean upperCase = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);

			if (c == SEPARATOR) {
				upperCase = true;
			} else if (upperCase) {
				sb.append(Character.toUpperCase(c));
				upperCase = false;
			} else {
				sb.append(c);
			}
		}

		return sb.toString();
	}

	/**
	 * 驼峰命名法工具
	 *
	 * @return toCamelCase("hello_world") == "helloWorld"
	 *         toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 *         toUnderScoreCase("helloWorld") = "hello_world"
	 */
	public static String toCapitalizeCamelCase(String s) {
		if (s == null) {
			return null;
		}
		s = toCamelCase(s);
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	/**
	 * 驼峰命名法工具
	 *
	 * @return toCamelCase("hello_world") == "helloWorld"
	 *         toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 *         toUnderScoreCase("helloWorld") = "hello_world"
	 */
	public static String toUnderScoreCase(String s) {
		if (s == null) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		boolean upperCase = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);

			boolean nextUpperCase = true;

			if (i < (s.length() - 1)) {
				nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
			}

			if ((i > 0) && Character.isUpperCase(c)) {
				if (!upperCase || !nextUpperCase) {
					sb.append(SEPARATOR);
				}
				upperCase = true;
			} else {
				upperCase = false;
			}

			sb.append(Character.toLowerCase(c));
		}

		return sb.toString();
	}

	/**
	 * 转换为JS获取对象值，生成三目运算返回结果
	 *
	 * @param objectString
	 *            对象串 例如：row.user.id
	 *            返回：!row?'':!row.user?'':!row.user.id?'':row.user.id
	 */
	public static String jsGetVal(String objectString) {
		StringBuilder result = new StringBuilder();
		StringBuilder val = new StringBuilder();
		String[] vals = split(objectString, ".");
		for (int i = 0; i < vals.length; i++) {
			val.append("." + vals[i]);
			result.append("!" + (val.substring(1)) + "?'':");
		}
		result.append(val.substring(1));
		return result.toString();
	}

}
